﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyDotTuyenSinh : QldtSdh.GUI.Common.BaseDialog
    {
        public FormQuanLyDotTuyenSinh()
        {
            InitializeComponent();
            gridData.AddRowIndexing();
            InitGrid();
            LoadGrid();
        }

        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
            colNamTuyenSinh.DataPropertyName = "NamTuyenSinh";
            colDot.DataPropertyName = "DotTuyenSinh1";
        }

        void LoadGrid()
        {
            bdDotTuyenSinh.DataSource = BizDotTuyenSinh.GetList();
            gridData.DataSource = bdDotTuyenSinh;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new FormQuanLyDotTuyenSinh_ChiTiet();
            frm.OnChanged += (o, t) =>
            {
                LoadGrid();
            };
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var current = bdDotTuyenSinh.Current as DotTuyenSinh;
            if (current != null)
            {
                var frm = new FormQuanLyDotTuyenSinh_ChiTiet(current);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    LoadGrid();
                }
            }
        }

        private void bdCoSoDaoTao_CurrentChanged(object sender, EventArgs e)
        {
            var current = bdDotTuyenSinh.Current as DotTuyenSinh;
            if (current != null)
            {
                btnSua.Enabled = btnXoa.Enabled = true;
            }
            else
            {
                btnSua.Enabled = btnXoa.Enabled = false;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var current = bdDotTuyenSinh.Current as DotTuyenSinh;
            if (current != null)
            {
                if (DialogUtils.ShowOKCancel(NOTIFY_CONSTANT_STRING.DELETE) == System.Windows.Forms.DialogResult.OK)
                {
                    var rs = BizDotTuyenSinh.Delete(current);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                        bdDotTuyenSinh.RemoveCurrent();
                    else if (rs == CODE_RESULT_RETURN.ThatBai)
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_XOA, ENTITY_NAME.DOTTUYENSINH));
                }
            }
        }
    }
}
